How-To: Control a PoppyCreature using the visual programming language Snap! (a variant of Scratch)

Introduction

This notebook will describe how:

  • you can connect the visual programming language Snap! to a Poppy Creature
  • and how you can control it in real time using poppy's custom blocks.

Snap! is a "very powerful visual, drag-and-drop programming language. It is an extended reimplementation of Scratch (a project of the Lifelong Kindergarten Group at the MIT Media Lab) that allows you to Build Your Own Blocks". It is an extremely efficient tool to learn how to program for kids or even college students and also a powerful prototyping method for artists.

Snap! is open-source and it is entirelly written in javascript, you only need a browser connected to the Poppy Creature webserver. No installation is required on your computer!

Note: We assume in this tutorial that you are familiar with the basic of Snap! or Scratch. If it's not the case you can find a lot of documentation online. We especially recommand the very good Snap! reference manual.

An example of the Snap! interface:

Comments, issues, improvements and updates can be sent directly on the dedicated section of the github issue tracker.

What's needed

First, if you do not know how to run an IPython Notebook please refer to our readme.

To follow this tutorial you will need:

Note: for this notebook we will use a simulated poppy humanoid in V-REP (see this v-rep notebook for details on how they can be installed and connected) but you can use any other creature (e.g. a real poppy ergo for instance). Only the configuration of the robot host will change (see details below).

Please refer to the install section if you don't know how to install these libraries or how to connect to your Poppy Creature.

Connect Snap! to a Poppy Creature

Before being able to control a simulated Poppy Creature with Snap! blocks a few steps are required:

  • First, we need to connect to a Poppy Creature (real or simulated).
  • Then, we need to tell pypot that we wan't to control it through Snap!.
  • Finally, connect to Snap! web interface (locally or online) and import the Poppy specific blocks.

Note: If you want to control a real Poppy Creature this is even simpler. As Poppy Creatures come with an embedded board with everything configured, you only need to connect to their webserver. Assuming that you are working with a Poppy-ErgoJr and that you are on the same network that your creature, you only have to connect to http://poppy-ergojr:8080/snap (see here for details).

Create a simulated Poppy Creature and connect it to Snap!

So, first we will create and launch the Robot Snap Server. In more details, we will:

We create a Poppy Humanoid using the approach discribed in here and specifies that we want to use Snap! to control it.


In [ ]:
from poppy.creatures import PoppyHumanoid

poppy = PoppyHumanoid(simulator='vrep', use_snap=True)

Note for advanced users: setting the use_snap arg to True basically creates a webserver which allows Snap! to get/post values from/to a Poppy Creature through pypot REST API. Then we use the http block to connect Snap! with the robot (you can refer to the section The Outside World from Snap! reference manual for more details). You can manually specify the host and port to which the server will be attached using snap_host and snap_port args. Here, we use the default values which bind the server to the localhost. Yet, this will not allow for external connections. You can use snap_host='0.0.0.0' to automatically attach the webserver to the IP of your machine. Hostnames can also be used, for instance Poppy Creatures usually provide an hostname such as host='poppy-humanoid.local'.


In [ ]:
poppy.snap.run()

Note for advanced user: the run method will run the server forever and thus block the main thread. This is here not a problem as you do not need to execute extra code for this tutorial. If you need to run other python code after, you can run this method inside a thread.

You can create a poppy creature from python with the above code, but you can also use a simple command on your terminal:

poppy-snap --vrep poppy-humanoid

If you want to try other configurations, look at the help of poppy-snap:

poppy-snap --help

Use Snap!

Now that we have anything we need to control our Poppy Creature, we just need to run Snap! on a web browser.

As stated above, Snap! is entirelly written in javascript and thus the only things needed to run it is a (not too old) web browser! You can run Snap! in two modes:

We will now detail on you can control your Poppy Creature via the two approaches:

Note that the online one is more straightforward and should thus be privileged except if you do not have an internet connection.

Alternative 1: Run Snap! Online

The last step required before actually making your robot moves in Snap! is to import our predefined blocks. Snap! provides a really simple way to do that: you just have to go to this url: http://snap.berkeley.edu/snapsource/snap.html#open:http://127.0.0.1:6969/snap-blocks.xml

Note the #open:http://... at the end of the url. It tells Snap! to automatically loads the blocks that can be found at the url: http://127.0.0.1:6969/snap-blocks.xml.

Note: if you changed the web server host, you need to change it in here as well. For instance if you use the Poppy Ergo default hostname you need to go to http://snap.berkeley.edu/snapsource/snap.html#open:http://poppy-ergojr.local:6969/snap-blocks.xml instead.

You should now see something like this in your browser (note importing the blocks may take a few seconds):

Alternative 2: Run Snap! locally

If you use Snap! locally instead, you will have to first:

  • launch Snap! by opening the snap.html file from you snap local folder
  • import the project with our specific blocks via the snap menu

The Poppy's specific blocks can be found on pypot/server/pypot-snap-blocks.xml directory in the pypot installation folder (its location will depend on your operating system and how you installed it). You can use the explorer/finder to find it.

Alternatively you can directly download it from the github repository. For instance:

wget https://raw.githubusercontent.com/poppy-project/pypot/master/pypot/server/pypot-snap-blocks.xml

Once imported you should see something like:

Using Snap! to make your Poppy Creature moves

You can see that our base project comes with a few specific blocks such as:

Those blocks can be used to respectively:

  • test if connection with poppy robot is working well
  • get a list of all motors name
  • get a list of all motors refered by an alias
  • get the value of a register motor (e.g. get motor "head_z" register "present_load")
  • get the index of a motor
  • get all alias avaible for the current robot
  • set a motor position in a specified time
  • turn a motor compliant or not
  • set a register of a motor (e.g. set motor "head_z" register "present_load" to 10)
  • create/attach a move to some motors (you have to create a move before to record or replay it)
  • stop the record of a move
  • start the record of a move
  • play a move at a defined speed
  • play a move in reverse at a defined speed
  • play concurently many moves
  • play sequentialy many moves

Other blocks are also available. Their behavior should be easily deduced from their name.

You can easily see all blocks relative to poppy in Snap! with the "find blocks" feature. You have to right-click in the left part of Snap! page and select "find blocks":

If you type robot on the search input, you will select all poppy blocks:

To control a motor via a slider you need to:

  • first, make a variable - we will call it head position
  • right click on it and use the slider option
  • change the slider min/max to (-50, 50)

Then, connect it to a motor:

  • use the motor(s) goto position block
  • put it inside a forever loop
  • add a wait for performance issue

Project example: orchestration of move records

On the pypot install directory pypot/server/pypot-snap-record-orchestration-demo.xml (its location will depend on your operating system and how you installed it), you can find a project tutorial of how to make orchestration of move record. Alternatively you can directly download it from the github repository:

wget https://raw.githubusercontent.com/poppy-project/pypot/master/pypot-snap-record-orchestration-demo.xml

or getting it from Snap! cloud.

This project will guide you step by step on how to use Snap! to record and play many nested move.

Project example: apply a sinus on a few motors

Demonstration Video